<?php
class FilterMenuForm extends Form
{
	private $min = 0,$max = 0,$filter_groups = array(),$filter_get = array(),$filter_price = array(),$price_cout_str = '';
	
	function __construct()
	{				
		Form::Form('FilterMenuForm');
		
		$this->filter_get	=	Url::get('filter');
		
		if(CGlobal::$curCategory && isset(CGlobal::$filters[CGlobal::$curCategory]))
		{
			$this->filter_groups	=	CGlobal::$filters[CGlobal::$curCategory];
		}
		
		
		#############################################################################################################################################
		#Search condition
		
		if(CGlobal::$curLevel2Cat)
		{
			CGlobal::$item_condition	= "level_2_catid = ".CGlobal::$curLevel2Cat['id']." AND ";
		}
		elseif(CGlobal::$curMainCat){
			CGlobal::$item_condition	= "level_1_catid = ".CGlobal::$curMainCat['id']." AND ";
		}
		
		CGlobal::$item_condition .="status = 1";//CGlobal::$item_condition .="status = 1 AND filter_ids != ''";				
		
		$filter_get 		= $this->filter_get;
		$this->filter_get 	= array();
		
		$filter_str = '';
		
		if($this->filter_groups)
		{
			foreach ($this->filter_groups as $fgid => $gfilter)
			{
				if($filter_get && isset($filter_get[$fgid]) && $filter_get[$fgid])
				{
					$fid = $filter_get[$fgid];
					
					if($gfilter['filters'] && in_array($fid,array_keys($gfilter['filters'])))
					{
						CGlobal::$no_paging = true;
						
						$this->filter_get[$fgid] = $fid;
						
						$filter_str .= ($filter_str==''?'+':' +')."g{$fgid}f{$fid}f";
					}
				}
			}
		}
											
		if(CGlobal::$curCategory && isset(CGlobal::$allCategories[CGlobal::$curCategory]))
		{
			$price_str = CGlobal::$allCategories[CGlobal::$curCategory]['price_str'];
			
			if($price_str)
			{
				$filter_price = array();
				
				$arr = explode(',',$price_str);
				
				$link = WEB_DIR."c".CGlobal::$curCategory."/".Url::get('ebname');
					
				if($this->filter_get)
				{
					foreach ($this->filter_get as $get_gid => $get_fid)
					{
						$link .= "&filter[{$get_gid}]=$get_fid";
					}
				}
				
				//SELECT 	SUM((price < 2000000 AND currency_id=1) OR ((price* 18000)< 200000 AND currency_id=2)) AS filter,  
				//SUM((price >= 2000000 AND price< 5000000  AND currency_id=1) OR ((price* 18000) >= 200000 AND (price* 18000) < 500000 AND currency_id=2)) AS filter
				//FROM products
				$last_price = 0;
				
				
				$get_min = (int)Url::get('min',0);
				$get_max = (int)Url::get('max',0);
				
				$i=1;
				foreach ($arr as $price)
				{
					if($last_price == 0)
					{
						$this->price_cout_str.=($this->price_cout_str != '' ? ',' : '')." SUM((price < $price AND currency_id=1) OR ((price * ".CGlobal::$exchange.") < $price AND currency_id=2)) AS `0`";
					}
					else{
						$this->price_cout_str.=($this->price_cout_str != '' ? ',' : '')." SUM((price >= $last_price AND price < $price AND currency_id=1) OR ((price * ".CGlobal::$exchange.") >= $last_price AND (price * ".CGlobal::$exchange.") < $price AND currency_id=2)) AS `$last_price`";
					}
					
					if($get_min == $last_price && $get_max == $price)//Được chọn
					{
						$this->min = $get_min;
						$this->max = $get_max;
					}
					
					$filter_price[] = array(	
											'min'=>$last_price,'max'=>$price
											);
											
					$last_price = $price;
					
					$i++;
				}
				
				if($last_price)
				{
					if($get_min == $last_price && $get_max == 0)//Được chọn
					{
						$this->min = $get_min;
						$this->max = 0;
					}
					
					$this->price_cout_str.=($this->price_cout_str != '' ? ',' : '')."SUM((price >= $last_price AND currency_id=1) OR ((price * ".CGlobal::$exchange.") >= $last_price AND currency_id=2)) AS `$last_price`";
					
					$filter_price[] = array(	
											'min'=>$last_price,'max'=>0
											);
											
					$last_price = $price;
				}
				
				if($this->min)
				{
					CGlobal::$item_condition .=" AND ((price >= ".$this->min." AND currency_id=1) OR ((price * ".CGlobal::$exchange.") >= ".$this->min." AND currency_id=2))";
					
				}
				
				if($this->max)
				{
					CGlobal::$item_condition .=" AND ((price < ".$this->max." AND currency_id=1) OR ((price * ".CGlobal::$exchange.") < ".$this->max." AND currency_id=2))";
				}
			}
		
			if($filter_str)
			{
				CGlobal::$item_condition .=" AND MATCH(filter_search) AGAINST ('$filter_str' IN BOOLEAN MODE)";
			}
			
			#############################################################################################################################################
			
			
			if($this->price_cout_str)
			{
				$this->filter_price = array(
										'name' 		=> "Giới hạn theo giá (VNĐ)",
										'filters' 	=> array()
										);
										
				$price_cout_arr = DB::fetch("SELECT ".$this->price_cout_str." FROM item WHERE ".CGlobal::$item_condition);
				
				if($filter_price)
				{
					foreach($filter_price as $fprice)
					{
						$min = $fprice['min'];
						$max = $fprice['max'];
							
						if($price_cout_arr[$min])
						{
							if($min && $max)
							{
								$link2 	= "$link&min={$min}&max={$max}";
								$name 	= AZLib::money($min)." - ".AZLib::money($max);
							}
							elseif(!$max)
							{
								$link2 = "$link&min={$min}";
								$name 	= "Trên ".AZLib::money($min);
							}
							elseif(!$min)
							{
								$link2 = "$link&max={$max}";
								$name 	= "Dưới ".AZLib::money($max);
							}
							
							if($this->min == $min && $this->max == $max)//Đc chọn
							{
								$this->filter_price['filters'][] = array(
																		'link' 		=> $link,
																		'name' 		=> $name,
																		'count' 	=> $price_cout_arr[$min],
																		'remove' 	=> 1,
																	);
							}
							else 
							{
								$this->filter_price['filters'][] = array(
																		'link' 		=> $link2,
																		'name' 		=> $name,
																		'count' 	=> $price_cout_arr[$min],
																		'remove' 	=> 0,
																	);
							}
						}
					}
				}
			}
		}
	}
	
	function draw()
	{
		global $display;
		
		if(CGlobal::$curCategory)
		{
			$filter_groups = array();
			
			
			$filter_ids = trim(DB::fetch("SELECT GROUP_CONCAT(filter_ids SEPARATOR ',') AS filter_ids FROM (SELECT filter_ids FROM item WHERE ".CGlobal::$item_condition." AND filter_ids != '') AS filter",'filter_ids',''));
		
			$pf_array = array();
			
			if($filter_ids != '')
			{
				$f_array = array_count_values(explode(',',$filter_ids));
				
				foreach ($f_array as $fid=>$count)
				{
					if(isset(CGlobal::$allFilters[$fid]))
					{
						$fgid = CGlobal::$allFilters[$fid]['fgid'];
						
						if(isset(CGlobal::$all_gfilters[$fgid]))
						{
							$pf_array[$fgid][$fid] = $count;
						}
					}
				}
			}
			
			//Tạo Menu Filter
			if($this->filter_groups)
			{
				foreach ($this->filter_groups as $fgid => $gfilter)
				{
					if(isset($pf_array[$fgid]))
					{
						if ($gfilter['filters'])
						{
							$group_filter = array();
							
							foreach ($gfilter['filters'] as $fid=>$filter)
							{
								if(isset($pf_array[$fgid][$fid]))
								{
									
									if(!$group_filter)
									{
										$group_filter = array(
																'name' 		=> CGlobal::$all_gfilters[$fgid]['name'],
																'filters' 	=> array(),
																);
									}
									
									$link = '';
									
									if(isset($this->filter_get[$fgid]) && $this->filter_get[$fgid] == $filter['id'])
									{
										$link_name = WEB_DIR."c".CGlobal::$curCategory."/".Url::get('ebname');
										
										foreach ($this->filter_get as $get_gid => $get_fid)
										{
											if($get_gid != $fgid)
											{
												$link_name .= "&filter[{$get_gid}]=$get_fid";
											}
										}
										
										if($this->min)
										{
											$link_name .= "&min=".$this->min;
										}
										if($this->max)
										{
											$link_name .= "&max=".$this->max;
										}
										
										$group_filter['filters'] = array(
																			$fid => array(
																							'link' 		=> $link_name,
																							'name' 		=> $filter['name'],
																							'count' 	=> $pf_array[$fgid][$fid],
																							'remove' 	=> 1,
																						)
																			);
										break;
									}
									else
									{
										$link = WEB_DIR."c".CGlobal::$curCategory."/".Url::get('ebname');
										
										if($this->filter_get)
										{
											foreach ($this->filter_get as $get_gid => $get_fid)
											{
												if($get_gid != $fgid)
												{
													$link .= "&filter[{$get_gid}]=$get_fid";
												}
											}
										}
										
										$link .= "&filter[{$fgid}]={$filter['id']}";//$link = str_replace(array('?filter=&','?filter=','&filter='),array('?','',''),$link);
										
										if($this->min)
										{
											$link .= "&min=".$this->min;
										}
										if($this->max)
										{
											$link .= "&max=".$this->max;
										}
									}
									
									$group_filter['filters'][$fid] =array(
																				'link' 		=> $link,
																				'name' 		=> $filter['name'],
																				'count' 	=> $pf_array[$fgid][$fid],
																				);
																				
								}
							}
							
							if($group_filter)
							{
								$filter_groups[$fgid] = $group_filter;
							}
						}
					}
				}
			}
			
			$display->add('url_filter' ,WEB_DIR."c".CGlobal::$curCategory."/".Url::get('ebname'));
			$display->add('filter_groups' ,$filter_groups);
			
			$display->add('filter_price' ,$this->filter_price);
		}
		
		
		
		if(User::have_permit(ADMIN_ITEM))
		{
			if(CGlobal::$curCategory)
				$display->add('edit_filter' ,"<a href='".Url::build('category',array("cmd"=>'edit','id'=>CGlobal::$curCategory))."' target='_blank'>Quản trị Thuộc tính</a>");
			else
				$display->add('edit_filter' ,"<a href='".Url::build('category',array("cmd"=>'filter_list'))."' target='_blank'>Quản trị Thuộc tính</a>");
		}
		
		$display->output("FilterMenu");
	}
}
?>